package edu.ccut.saturn.studio.export.model;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;

import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.studio.component.model.Component;
import edu.ccut.saturn.studio.component.model.ComponentModel;
import edu.ccut.saturn.studio.component.model.Parameter;
import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;

/**
 * 生成基础构件的具体实现类
 * 
 * @author 杨艳斌
 * 
 * @version 1.1 2009.12.11
 */

public class Comp {
	
	private static String filename;
	private static String info;
	
	
	/**
	 * 创建基础构件的html文档
	 * 
	 * @param file,filePath
	 * @return String
	 * @throws XMLException
	 */
	public static String createComponentHtml(File file,String filePath) throws XMLException{
		
		filename = file.getName().replace(".", "-");
		Document doc = SaturnW3CUtil.getDocument(file.getAbsolutePath());
		ComponentModel model = new ComponentModel(doc);
		Component comp = model.getComponent();
		generateInfo(comp, filePath);
		
		return info;
	}
	
	/**
	 * 创建逻辑构件的html文档的具体实现方法
	 * 
	 * @param comp,filePath
	 * @return 
	 */
	private static void generateInfo(Component comp, String filePath) {
		
		Map<String, List<?>> parameters = new HashMap<String, List<?>>();
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		String tempalatePath = "template/export/component";
		List<Parameters> values =  new ArrayList<Parameters>();
		Parameters para = new Parameters();
		info = comp.getDescription();
		
		para.setKey(comp.getKey());
		para.setName(comp.getName());
		para.setClassname(comp.getClassStr());
		para.setDescription(comp.getDescription());
		para.setLoadstart(comp.getLoadStart());
		
		//参数列表的添加
		for (IParameterInfo parameterInfo : comp.getParameterInfos()) {
			Parameters infos = new Parameters();
			Parameter paras = (Parameter) parameterInfo;

			infos.setId(paras.getId());
			infos.setName(paras.getName());
			infos.setClassType(paras.getClassStr());
			infos.setPassingtype(transfertype(paras.getPassingType()));
			infos.setExperssion(paras.getValueKey());
			infos.setScope(transfer(paras.getScope()));
			infos.setDescription(paras.getDescription());
			
			values.add(infos);
		}
		
		basicvaluse.add(para);
		parameters.put("parameters", values);
		parameters.put("basicvaluse", basicvaluse);

		FreemarkerUtils.writeTemplateToFile(tempalatePath ,parameters, filePath 
				+ File.separator + filename + ".html");
	}
	
	/**
	 * 基础构件中类型的转换
	 * 
	 * @param passingType
	 * @return String
	 */
	private static String transfertype(String passingType) {
		if(passingType.equals("in-out")) {
			return Messages.getString("Comp.InOut"); //$NON-NLS-1$
		} else if (passingType.endsWith("in")) {
			return Messages.getString("Comp.In"); //$NON-NLS-1$
		} else {
			return Messages.getString("Comp.Out");  //$NON-NLS-1$
		}
	}
	
	/**
	 * 基础构件中数据域的转换
	 * 
	 * @param type
	 * @return String
	 */
	private static String transfer(int type) {
		if (type == 1) {
			return "logic";
		} else if (type == 2){
			return "request";
		} else if (type == 3) {
			return "session";
		} else {
			return "application"; 
		}
	}
}
